package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/4 8:14 AM
 */
public class LC_456 {
    public static void main(String[] args) {
        System.out.println(
                new LC_456()
                        .find132pattern(
                                new int[]{
                                        1, 2, 4, 3, 5
                                }
                        )
        );
    }

    public boolean find132pattern(int[] nums) {
        if (nums.length < 3) {
            return false;
        }
        int last = Integer.MIN_VALUE;
        Stack<Integer> stack = new Stack<>();
        for (int i = nums.length - 1; i > -1; i--) {
            int num = nums[i];
            if (num < last) {
                return true;
            }
            while (!stack.empty() && stack.peek() < num) {
                last = stack.pop();
            }
            stack.push(num);
        }

        return false;
    }
}
